package main

import (
	"fmt"
	"gitee.com/snxamdf/wui/logs"
	"gitee.com/snxamdf/wui/util"
	"os"
	"path/filepath"
	"strings"
)

var (
	gosrc         = "src"
	resource      = "resources"
	syso          = "rsrc.syso"
	manifest      = "application.exe.manifest"
	assesStatic   = fmt.Sprintf("%s/static", resource)
	assesLib      = fmt.Sprintf("%s/lib", assesStatic)
	assesLibJs    = fmt.Sprintf("%s/js", assesLib)
	assesLibCss   = fmt.Sprintf("%s/css", assesLib)
	assesLibImage = fmt.Sprintf("%s/image", assesLib)

	//1.1=webview-1.1
	//2.0=webview-2.0
	defaultVersion = "1.1"
	//类库
	cmdLibPath  = "cmd/lib"
	cmdLibJs    = fmt.Sprintf("%s/js", cmdLibPath)
	cmdLibCss   = fmt.Sprintf("%s/css", cmdLibPath)
	cmdLibImage = fmt.Sprintf("%s/image", cmdLibPath)
)

/*
	1. go get -u github.com/jteeuwen/go-bindata/...
	2. go build go-bindata 复制 go-bindata 到go/bin目录
	3. go-bindata -o=cmd/wui-install/asses.go -pkg=main cmd/lib/...
*/
func main() {
	logs.Info("项目初始化")
	currentPath, _ := os.Getwd()
	logs.Info("当前目录：", currentPath)
	logs.Info("读取lib文件并打包")
	logs.Info("初始化目录结构")
	createDir()
	logs.Info("复制输出依赖")
	copyLib()
	logs.Info("添加wui示例")
	writeDemo()
}

func copyLib() {
	writeLib(cmdLibPath, ".", "rsrc.syso")
	writeLib(cmdLibPath, ".", "app.exe.manifest")
	writeLib(cmdLibPath, ".", "webview.dll")
	writeLib(cmdLibPath, ".", "WebView2Loader.dll")

	writeLib(cmdLibPath, resource, "wui-config.json")

	writeLib(cmdLibJs, assesLibJs, "go-bind.js")
	writeLib(cmdLibJs, assesLibJs, "md5.js")
	writeLib(cmdLibJs, assesLibJs, "vue.v2.6.14.js")
	writeLib(cmdLibJs, assesLibJs, "vue-router.v2.8.1.u.js")
	writeLib(cmdLibCss, assesLibCss, "wui-widget.css")
	writeLib(cmdLibImage, assesLibImage, "*")
}

func writeLib(source, target, name string) {
	if name == "" {
		return
	} else if name == "*" {
		writeLibAll(source, target)
		return
	}
	logs.Info("参数：", source, target, name)
	source = fmt.Sprintf("%s/%s", source, name)
	logs.Info("源文件：", source)
	target = fmt.Sprintf("%s/%s", target, name)
	logs.Info("目标文件：", target)
	child, ok := _bintree.Children[source]
	if child == nil || !ok {
		logs.Info("获取输出文件失败：", source, ok)
		logs.Info()
		return
	}
	if byt, err := child.Func(); err == nil {
		dir, _ := filepath.Split(target)
		err = os.MkdirAll(dir, 0777)
		if err != nil {
			logs.Info("输出文件失败：", target, err)
		} else {
			if !util.IsExist(target) {
				if err := os.WriteFile(target, byt, 0777); err != nil {
					logs.Info("输出文件失败：", target, err)
				} else {
					logs.Info("输出文件成功：", target, err)
				}
			} else {
				logs.Info("输出文件已经存在：", target, err)
			}
		}
		logs.Info()
	} else {
		logs.Info("输出文件失败：", target, err)
		logs.Info()
	}
}

func writeLibAll(source, target string) {
	children := _bintree.Children
	for filePath, _ := range children {
		if strings.Contains(filePath, source) {
			dir, name := filepath.Split(filePath)
			dir = dir[0 : len(dir)-1]
			subDir := dir[len(source):]
			source = dir
			target = target + subDir
			writeLib(source, target, name)
		}
	}
}

func createDir() {
	mkdir(gosrc)
	mkdir(resource)
	mkdir(fmt.Sprintf("%s/static", resource))
	mkdir(fmt.Sprintf("%s/template", resource))
	mkdir(fmt.Sprintf("%s/css", assesStatic))
	mkdir(fmt.Sprintf("%s/js", assesStatic))
	mkdir(fmt.Sprintf("%s/image", assesStatic))
	mkdir(assesLib)
	mkdir(assesLibJs)
	mkdir(assesLibCss)
	mkdir(assesLibImage)
	logs.Info()
}

func mkdir(path string) {
	if !util.IsExist(path) {
		err0 := os.Mkdir(path, 0777)
		err1 := os.Chmod(path, 0777)
		logs.Info("创建目录：", path, err0, err1)
	}
}
